package com.jnr.huatu.activity;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;

import com.jnr.huatu.R;
import com.jnr.huatu.utils.DoubleDatePickerDialog;

import java.util.Calendar;

import static android.R.attr.startYear;

/**
 * 时间选择器
 * @author：景辉
 * created at 2017/2/15 0015 上午 11:37
 */
public class DataActivity extends AppCompatActivity {
    Button btn;
    TextView et;
    private TextView tvWeek;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data);
        btn = (Button) findViewById(R.id.dateBtn);
        et = (TextView) findViewById(R.id.et);
        tvWeek = (TextView) findViewById(R.id.tv_week);

        btn.setOnClickListener(new View.OnClickListener() {
            Calendar c = Calendar.getInstance();

            @Override
            public void onClick(View v) {
                // 最后一个false表示不显示日期，如果要显示日期，最后参数可以是true或者不用输入
            new DoubleDatePickerDialog(DataActivity.this, 0, new DoubleDatePickerDialog.OnDateSetListener() {

                    @Override
                    public void onDateSet(DatePicker startDatePicker, int startYewar, int startMonthOfYear,
                                          int startDayOfMonth) {
                        String textString = String.format("开始时间：%d-%d-%d", startYear,
                                startMonthOfYear+1, startDayOfMonth);
                        et.setText(textString);
                        Log.i("TAG", "年: ++++++++++++"+startYear);
                        Log.i("TAG", "月: ++++++++++++"+(startMonthOfYear+1));
                        Log.i("TAG", "日: ++++++++++++"+startDayOfMonth);
                        int y=startYear;
                        int m=startMonthOfYear+1;
                        int d=startDayOfMonth;
                        if (m==1||m==2){
                            m=m+12;
                            y=y-1;
                        }
                        int w=(d+1+2*m+3*(m+1)/5+y+(y/4)-y/100+y/400)%7;
                        String myWeek = null;
                        switch(w)
                        {
                            case 0:
                                myWeek="日";
                                break;
                            case 1:
                                myWeek="一";
                                break;
                            case 2:
                                myWeek="二";
                                break;
                            case 3:
                                myWeek="三";
                                break;
                            case 4:
                                myWeek="四";
                                break;
                            case 5:
                                myWeek="五";
                                break;
                            case 6:
                                myWeek="六";
                                break;
                            default:
                                break;
                        }
                        tvWeek.setText(myWeek);
                    }
                }, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), false).show();
            }
        });
    }
}
